import time

import allure
from conftest import login_suc
from key_word.key_word import KeyWord
from locator.goods import *
from locator.login import *
from logic.banner import BannerPage
from logic.library import Library


class GoodsPage(KeyWord):
    # 前置条件
    def setup(self):
        # 前置-登录
        login_suc(browser=self.driver)
        # 前置-点击左侧banner
        BannerPage(self.driver).click_banner('商品', "商品列表")

    # 添加商品页
    def add_goods(self, browser, goods_name, goods_sec_name, goods_kind, category_value, sort_value,
                  edu_discount_num, limit_table, limit_num, express_amount, goods_tags,
                  specs_content, pages, views, goods_description):
        self.change_into_iframe(*page_right_iframe)  # 切换右侧列表iframe
        with allure.step('点击添加按钮'):
            self.click_el_until_visibility(*page_goods_add_goods_btn)

        self.change_into_iframe(*page_add_spu_iframe)  # 切换表单iframe
        with allure.step('输入商品名称SPU'):
            self.send_content(page_goods_goods_name[0], page_goods_goods_name[1], goods_name)

        with allure.step('输入商品副标题'):
            self.send_content(page_goods_goods_sec_name[0], page_goods_goods_sec_name[1], goods_sec_name)

        with allure.step('输入商品类型：1=实物，2=虚拟'):
            page_goods_kind = [By.ID, f"goodsKind{goods_kind}"]
            self.click_el_until_visibility(*page_goods_kind)

        with allure.step('选择商品分类'):
            self.click_el_until_visibility(*page_goods_category_list)  # 商品分类下拉点击
            page_goods_category_select = [By.XPATH, f"//span[contains(text(),'{category_value}')]/.."]
            self.click_el_until_visibility(*page_goods_category_select)  # 选择商品分类

        with allure.step('输入排序值'):
            self.send_content(page_goods_sort[0], page_goods_sort[1], sort_value)

        with allure.step('选择教育优惠：是=1，否=2'):
            page_goods_edu_discount = [By.ID, f"eduDiscount{edu_discount_num}"]
            self.click_el_until_visibility(*page_goods_edu_discount)

        with allure.step('选择教育优惠：不限购=1，限购=2'):
            page_goods_limit_table = [By.ID, f"limitNum{limit_table}"]
            self.click_el_until_visibility(*page_goods_limit_table)
            if limit_table == "2":
                self.send_content(page_goods_limit_num[0], page_goods_limit_num[1], limit_num)

        with allure.step('输入快递运费'):
            self.send_content(page_goods_express_amount[0], page_goods_express_amount[1], express_amount)

        with allure.step('输入商品标签值'):
            for tag in goods_tags:  # 循环获取商品标签值
                self.send_keys_enter(page_goods_tags[0], page_goods_tags[1], tag)

        with allure.step('选择预置规格并输入具体规格'):
            for spec_name, spec_content in specs_content.items():
                self.click_el_until_visibility(*page_goods_pre_specs)  # 点击预置规格
                page_goods_specs_name = [By.XPATH,
                                         f"//div[@class='dropdown-menu show']//a[text()='{spec_name}']"]  # 定位输入的规格名称
                self.click_el_until_visibility(*page_goods_specs_name)  # 点击具体规格
                page_goods_specs_content = [By.XPATH, f"//label[text()='{spec_name}']/..//input"]  # 获取指定规格输入框
                for content in spec_content:
                    self.send_keys_enter(page_goods_specs_content[0], page_goods_specs_content[1], content)  # 循环输入规格值

        with allure.step('选择商品图片'):
            self.scroll_by_amount(936, 1436)  # 滚动页面到保存按钮出现
            self.click_el_until_visibility(page_goods_page[0], page_goods_page[1])  # 点击图片上传
            with allure.step("添加规格图片"):
                Library(browser).add_material(pages)

        with allure.step('选择商品视频'):
            self.change_parent_frame()  # 切回父级frame
            self.scroll_by_amount(936, 1436)  # 滚动页面到保存按钮出现
            self.click_el_until_visibility(*page_goods_view)  # 点击视频上传
            with allure.step("添加商品视频"):
                Library(browser).add_material(views)

        self.change_parent_frame()  # 切回父级frame
        self.scroll_by_amount(936, 1436)  # 滚动页面到保存按钮出现

        with allure.step('输入商品介绍'):
            self.send_content(page_goods_description[0], page_goods_description[1], goods_description)

        with allure.step('点击保存'):
            self.click_el_until_visibility(*page_goods_save_btn)

    # 商品查询
    def select_goods(self, goods_name):
        with allure.step('输入商品名称'):
            self.send_content(page_goods_select_goodsName[0], page_goods_select_goodsName[1], goods_name)
        with allure.step("点击查询按钮"):
            self.click_el_until_visibility(*page_goods_select_btn_search)

    # 点击上下架
    def change_goods_status(self, goods_name, goods_status_input):
        with allure.step(f"点击{goods_status_input}按钮"):
            self.up_status = [By.XPATH,
                              f"//span[text()='{goods_name}']/..//..//..//div[@class='goods-info-detail']/..//..//a[text()='{goods_status_input}']"]
            self.click_el_until_visibility(*self.up_status)
        with allure.step(f"点击确认{goods_status_input}按钮"):
            time.sleep(2)
            self.click_el_until_visibility(*page_goods_list_status_btn_true)
            time.sleep(2)

    # 点击编辑SKU按钮
    def click_edit_sku_btn(self):
        with allure.step("点击编辑SKU按钮"):
            self.click_el_until_visibility(*page_sku_add_btn)
            # 切换sku添加表单iframe
            self.change_into_iframe(*page_sku_add_iframe)
        with allure.step("点击创建全部规格按钮"):
            self.click_el_until_visibility(*page_sku_add_all_size)

    # 添加SKU
    def add_sku(self, browser, size_num, pages, sku_code, sku_material_code, sku_sales_price, sku_original_price,
                sku_edu_off_price, sku_limit_num):
        with allure.step(f"添加第{size_num}个SKU数据"):
            page_sku_add_size_page = [By.XPATH, f"//tbody[@id='goods-detail-tb-body']/tr[{size_num}]//img"]
            self.click_el_until_visibility(*page_sku_add_size_page)
            with allure.step("添加规格图片"):
                Library(browser).add_material(pages)
            self.change_parent_frame()
            # SKU规格商品编码
            page_sku_goods_code = [By.XPATH,
                                   f"//tbody[@id='goods-detail-tb-body']//tr[{size_num}]//td[5]//input[@name='code']"]
            self.send_content(page_sku_goods_code[0], page_sku_goods_code[1], sku_code)
            # ERP商品物料编码
            page_sku_goods_material_code = [By.XPATH,
                                            f"//tbody[@id='goods-detail-tb-body']//tr[{size_num}]//td[6]//input[@name='thirdCode']"]
            self.send_content(page_sku_goods_material_code[0], page_sku_goods_material_code[1], sku_material_code)
            # 销售价格
            page_sku_goods_sales_price = [By.XPATH,
                                          f"//tbody[@id='goods-detail-tb-body']//tr[{size_num}]//td[7]//input[@name='payAmount']"]
            self.send_content(page_sku_goods_sales_price[0], page_sku_goods_sales_price[1], sku_sales_price)
            # 商品原价
            page_sku_goods_original_price = [By.XPATH,
                                             f"//tbody[@id='goods-detail-tb-body']//tr[{size_num}]//td[9]//input[@name='totalAmount']"]
            self.send_content(page_sku_goods_original_price[0], page_sku_goods_original_price[1],
                              sku_original_price)
            # 教育优惠价格
            page_sku_goods_edu_off_price = [By.XPATH,
                                            f"//tbody[@id='goods-detail-tb-body']//tr[{size_num}]//td[10]//input[@name='eduDiscountAmount']"]
            self.send_content(page_sku_goods_edu_off_price[0], page_sku_goods_edu_off_price[1],
                              sku_edu_off_price)
            # 每人限购
            page_sku_goods_limit_num = [By.XPATH,
                                        f"//tbody[@id='goods-detail-tb-body']//tr[{size_num}]//td[11]//input[@name='limitNum']"]
            self.send_content(page_sku_goods_limit_num[0], page_sku_goods_limit_num[1], sku_limit_num)
        with allure.step("点击保存按钮"):
            self.click_el_until_visibility(*page_sku_save_btn)


